package com.kinon.gfcanteen_v1.network;

import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.kinon.gfcanteen_v1.app.MyApp;
import com.kinon.gfcanteen_v1.util.MyShowDialog;

import io.reactivex.subscribers.ResourceSubscriber;
import retrofit2.adapter.rxjava.HttpException;


/**
 * Created by lhqq on 2017-12-20.
 */

public abstract class CommonSubscriber<T> extends
        ResourceSubscriber<T> {

    private TextView mView;
    private String mErrorMsg;
    private boolean isShowErrorState = true;
    private boolean isCloseDialog = false;

    protected CommonSubscriber(){

    }
    protected CommonSubscriber(boolean isCloseDialog){
        this.isCloseDialog=isCloseDialog;
        if(this.isCloseDialog){
            MyShowDialog.closeLoadingDialog();
        }

    }

    protected CommonSubscriber(TextView view){
        this.mView = view;
    }

    protected CommonSubscriber(TextView view, String errorMsg){
        this.mView = view;
        this.mErrorMsg = errorMsg;
    }

    protected CommonSubscriber(TextView view,
                               boolean isShowErrorState){
        this.mView = view;
        this.isShowErrorState = isShowErrorState;
    }

    protected CommonSubscriber(TextView view, String errorMsg,
                               boolean isShowErrorState){
        this.mView = view;
        this.mErrorMsg = errorMsg;
        this.isShowErrorState = isShowErrorState;
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onError(Throwable t) {
        MyShowDialog.closeLoadingDialog();
//        if (mView == null) {
//            return;
//        }

        Log.e("http", "onError=="+t.getMessage());
        if (mErrorMsg != null && !TextUtils.isEmpty(mErrorMsg)) {
//            mView.showErrorMsg(mErrorMsg);
//            mView.setText(mErrorMsg);
            Toast.makeText(MyApp.getInstance(),
                    mErrorMsg, Toast.LENGTH_SHORT).show();
            onFail(t,1000,mErrorMsg);
        } else if (t instanceof ApiException) {
//            mView.showErrorMsg(e.toString());
//            mView.setText(t.toString());

            Toast.makeText(MyApp.getInstance(),
                    t.getMessage(), Toast.LENGTH_SHORT).show();
            onFail(t,1001,t.getMessage());
        } else if (t instanceof HttpException) {
//            mView.showErrorMsg("数据加载失败ヽ(≧Д≦)ノ");
//            mView.setText("数据加载失败ヽ(≧Д≦)ノ");
            Toast.makeText(MyApp.getInstance(),
                    "数据加载失败ヽ(≧Д≦)ノ", Toast.LENGTH_SHORT).show();
            onFail(t,1002,"数据加载失败ヽ(≧Д≦)ノ");
        } else {
//            mView.setText("未知错误ヽ(≧Д≦)ノ");
            Toast.makeText(MyApp.getInstance(),
                    "未知错误ヽ(≧Д≦)ノ", Toast.LENGTH_SHORT).show();
            onFail(t,1003,"未知错误ヽ(≧Д≦)ノ");
//            mView.showErrorMsg("未知错误ヽ(≧Д≦)ノ");

//            LogUtil.d(e.toString());
        }
//        if (isShowErrorState) {
//            mView.stateError();
//        }
    }
     public abstract void onFail(Throwable t, int code, String msg );
}
